# Clears work environment
rm(list = ls())

# Sets working directory
setwd('C:/Users/Jason/Box Sync/Home Folder jdt34/733 - Maximum Likelihood Estimation/Lai & Slater 2006 replication')

# Loads data and libraries
load(file='cleanedLSdata.RData')
load(file='panel4_simulated_scenarios.RData')
loadPkg(packs)

l0l1ci = data.frame()
tmp = by(l0nb[,2], l0nb$regime, cimaker)
l0l1ci = rbind(l0l1ci, tmp[[5]], tmp[[4]], tmp[[3]], tmp[[2]], tmp[[1]])
tmp = by(l1nb[,2], l1nb$regime, cimaker)
l0l1ci = rbind(l0l1ci, tmp[[5]], tmp[[4]], tmp[[3]], tmp[[2]], tmp[[1]])
tmp = by(l0zi[,2], l0zi$regime, cimaker)
l0l1ci = rbind(l0l1ci, tmp[[5]], tmp[[4]], tmp[[3]], tmp[[2]], tmp[[1]])
tmp = by(l1zi[,2], l1zi$regime, cimaker)
l0l1ci = rbind(l0l1ci, tmp[[5]], tmp[[4]], tmp[[3]], tmp[[2]], tmp[[1]])
l0l1ci$regime = factor(c('Democracy', IVs[1:4]), levels=rev(c('Democracy', IVs[1:4])))
l0l1ci$scenmodel = factor(c(rep('Negative binomial: lagged DV = 0', 5), 
                            rep('Negative binomial: lagged DV = 1', 5), 
                            rep('Zero-inflated negative binomial: lagged DV = 0', 5), 
                            rep('Zero-inflated negative binomial: lagged DV = 1', 5)))
colnames(l0l1ci)[1:5] = c('low95', 'low90', 'est', 'upp90', 'upp95')
fourpreds = ggplot(data=l0l1ci, aes(x=regime, y=est, color=regime)) +
  geom_linerange(aes(ymin=low95, ymax=upp95)) +
  geom_errorbar(aes(ymin=low95, ymax=upp95), width=.25) +
  geom_point(size=4, shape=18) +
  geom_hline(x=0, linetype=2, alpha=.5) +
  facet_wrap(~scenmodel) +
  xlab('') +
  ylab('') +
  scale_color_manual(values=rev(panelcols)) +
  guides(color=F) +
  coord_flip() + 
  theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank())
ggsave(file='fourpreds.pdf', fourpreds, width=9, height=6, units='in')

save(file='panel6_scenario_confidence_intervals.RData', l0l1ci, fourpreds)
